package demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Test {

    private static final String[] SUITS = {"♥","♠","♣","♦"};//花色

  //生成52张牌
    public static List<Card> buyCard() {
        List<Card> cards = new ArrayList<>();
        for (int i = 0; i < SUITS.length; i++) {
            for(int j = 1; j <= 13; j++) {
                Card card = new Card(SUITS[i],j);//花色和对应点数
                cards.add(card);
            }
        }
        return cards;
    }
    //洗牌shuffle
    public static void shuffle(List<Card> cards) {
        Random random = new Random();

        for (int i = cards.size() - 1; i > 0; i--) {
            int j = random.nextInt(i);
            //交换
            Card tmp = cards.get(i);//中间变量
            cards.set(i,cards.get(j));//将i下标的值换位j下标的值
            cards.set(j,tmp);
        }
    }

    public static void main(String[] args) {
        List<Card> cards = buyCard();//接收生成的牌
        System.out.println(cards);
        System.out.println("洗牌");
        shuffle(cards);
        System.out.println(cards);

        //三个人每个人揭牌5张
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hand = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        //揭牌5次，每次三个人揭牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //揭牌动作
                Card card = cards.remove(0);
                //人揭牌到手
                hand.get(j).add(card);
            }
        }
        System.out.println("第一个人的牌" + hand1);
        System.out.println("第二个人的牌" + hand2);
        System.out.println("第三个人的牌" + hand3);
        System.out.println(cards);
    }
}
